﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DblogStudio.DAL.Models;

namespace DblogStudio.BLL
{
    public partial class BizPhanCongBaoTri
    {
        public static bool CheckPermissionForCongDoan(string maNhanVien, string maCongDoan, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return CheckPermissionForCongDoan(maNhanVien, maCongDoan, db);
                }
            else
            {
                var ls = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == maNhanVien && e.MaCongDoan == maCongDoan).ToList();
                if (ls != null && ls.Count > 0)
                    return true;
                else
                    return false;
            }
        }

        public static bool CheckPermissionForMaySanXuat(string maNhanVien, string maMaySanXuat, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return CheckPermissionForMaySanXuat(maNhanVien, maMaySanXuat, db);
                }
            else
            {
                var ls = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == maNhanVien && e.MaMaySanXuat == maMaySanXuat).ToList();
                if (ls != null && ls.Count > 0)
                    return true;
                else
                    return false;
            }
        }

        public static bool CheckPermissionForBoPhanMay(string maNhanVien, string maBoPhanMay, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return CheckPermissionForBoPhanMay(maNhanVien, maBoPhanMay, db);
                }
            else
            {
                var ls = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == maNhanVien && e.MaBoPhanMay == maBoPhanMay).ToList();
                if (ls != null && ls.Count > 0)
                    return true;
                else
                    return false;
            }
        }

        public static bool CheckPermissionForChiTietMay(string maNhanVien, string maChiTietMay, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return CheckPermissionForChiTietMay(maNhanVien, maChiTietMay, db);
                }
            else
            {
                var ls = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == maNhanVien && e.MaChiTietMay == maChiTietMay).ToList();
                if (ls != null && ls.Count > 0)
                    return true;
                else
                    return false;
            }
        }

        public static List<AppUser> GetList(string maChiTietMay)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var ds = db.PhanCongBaoTriDbSet.Where(e => e.MaChiTietMay == maChiTietMay).Select(e => e.MaNhanVien).ToList();
                return db.AppUserDbSet.Where(e => ds.Contains(e.UserName)).OrderBy(e => e.FullName).ToList();
            }
        }

        public static PhanCongBaoTri GetByID(string maPhanCong)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.PhanCongBaoTriDbSet.Where(e => e.MaPhanCong == maPhanCong).FirstOrDefault();
            }
        }

        public static PhanCongBaoTri IsExist(string maChiTietMay, string maNhanVien, string maBoPhanMay, string maMaySanXuat, string maCongDoan, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return IsExist(maChiTietMay, maNhanVien, maBoPhanMay, maMaySanXuat, maCongDoan, db);
                }
            else
            {
                return db.PhanCongBaoTriDbSet.Where(e => e.MaChiTietMay == maChiTietMay &&
                    e.MaNhanVien == maNhanVien &&
                    e.MaBoPhanMay == maBoPhanMay &&
                    e.MaMaySanXuat == maMaySanXuat &&
                    e.MaCongDoan == maCongDoan).FirstOrDefault();
            }
        }

        public static CODE_RESULT_RETURN Update(PhanCongBaoTri obj, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return Update(obj, db);
                }
            else
            {
                if (IsExist(obj.MaChiTietMay, obj.MaNhanVien, obj.MaBoPhanMay, obj.MaMaySanXuat, obj.MaCongDoan) != null)
                    return CODE_RESULT_RETURN.MaTrung;
                obj.MaPhanCong = Guid.NewGuid().ToString();
                try
                {
                    db.PhanCongBaoTriDbSet.Add(obj);
                    db.SaveChanges();
                    //NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    //BizNhatKyHoatDong.InitData<LichBaoTri>(nhatKy, null, obj.MaPhanCong, LOAIDOITUONG.PHÂN_CÔNG_BẢO_TRÌ, LOAITHAOTAC.Thêm);
                    //BizNhatKyHoatDong.Add(nhatKy, obj);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        public static CODE_RESULT_RETURN Delete(PhanCongBaoTri obj, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return Delete(obj, db);
                }
            else
            {
                var pc = IsExist(obj.MaChiTietMay, obj.MaNhanVien, obj.MaBoPhanMay, obj.MaMaySanXuat, obj.MaCongDoan);
                if (pc == null)
                    return CODE_RESULT_RETURN.ThatBai;
                int rs = db.Database.ExecuteSqlCommand(string.Format("DELETE FROM PhanCongBaoTri WHERE MaPhanCong ='{0}'", pc.MaPhanCong));
                //NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                //BizNhatKyHoatDong.InitData<LichBaoTri>(nhatKy, may, may.MaLichBaoTri, LOAIDOITUONG.LỊCH_BẢO_TRÌ, LOAITHAOTAC.Xóa);
                //BizNhatKyHoatDong.Add<NhiemVuBaoTri>(nhatKy, null);
                if (rs == 1) return CODE_RESULT_RETURN.ThanhCong;
                else
                    return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }

}
